package com.leyou.goods.controller;

import com.leyou.goods.service.GoodsHtmlService;
import com.leyou.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("item")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsHtmlService goodsHtmlService;

    @GetMapping("{id}.html")
    public String toItemPage(Model model,@PathVariable("id")Long id){
        Map<String, Object> map = this.goodsService.querySpuById(id);
        model.addAllAttributes(map);
        //页面静态化
        //this.goodsHtmlService.asyncExcute(id);
        return "item";
    }

    @GetMapping("spu/{id}")
    public ResponseEntity<Map<String,Object>> querySpuById(@PathVariable("id")Long id){
        Map<String, Object> map = this.goodsService.querySpuById(id);
        if(map==null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(map);
    }


}
